package com.fancymusic.controller;

import com.fancymusic.dao.IMusicDao;
import com.fancymusic.dao.ISingerDao;
import com.fancymusic.entity.BaseResult;
import com.fancymusic.entity.Music;
import com.fancymusic.entity.MusicType;
import com.fancymusic.entity.Singer;
import com.fancymusic.service.IMusicService;
import com.fancymusic.service.IMusicTypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class MusicController {

    @Autowired
    IMusicService iMusicService;

    @Autowired
    IMusicDao iMusicDao;

    @Autowired
    ISingerDao iSingerDao;

    @Autowired
    IMusicTypeService iMusicTypeService;

    @RequestMapping("getAllMusic")
    public PageInfo<Music> getAllMusic(@RequestParam("pageSize") Integer pageSize, @RequestParam("pageNum") Integer pageNum) {


        return iMusicService.getAllMusic(pageSize, pageNum);
    }

    @RequestMapping("getAllMusicByid")
    public Music getAllMusicByid(@RequestParam("id") Integer id) {

        return iMusicDao.getMusicByMid(id);
    }


    @RequestMapping("saveMusic")
    public BaseResult save(@RequestBody Map map) {

        BaseResult<String> baseResult = new BaseResult();
        System.out.println("====MusicController====map=====" + map);

        Music music = new Music();
        music.setMusicName((String) map.get("musicName"));
        List<Singer> singerList = iSingerDao.getSingerList();

        Singer sid = iSingerDao.findSingerBysid((Integer) map.get("musicId"));


//        if (sid.getSingerName().compareTo((String) map.get("singerName"))==0){
        music.setSinger(sid);


        List<MusicType> musicType = iMusicTypeService.getMusicType();
        music.setMusicType(musicType.get(0));


        iMusicService.save(music);

        System.out.println(music);

        baseResult.setState(true);
        baseResult.setMessage("添加成功");
        return baseResult;
    }


    @RequestMapping("deleteMusic")
    public BaseResult deleteMusic(@RequestParam("id") Integer id) {

        BaseResult<String> baseResult = new BaseResult();
        System.out.println("====MusicController====deleteMusic=====id===" + id);

        Music music = new Music();


        try {
            iMusicService.deleteMusic(id);

            baseResult.setState(true);
            baseResult.setMessage("删除成功");
            return baseResult;

        } catch (Exception e) {
            baseResult.setState(false);
            baseResult.setMessage("删除失败");
            return baseResult;
        }


    }

}
